package com.zeng.niceservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zeng.nicecore.constant.IgnoreConstant;
import com.zeng.nicecore.util.BeanUtil;
import com.zeng.nicecore.util.IgnoreStringUtil;
import com.zeng.nicedao.mapper.FileCategoryMapper;
import com.zeng.nicepojo.entity.FileCategory;
import com.zeng.nicepojo.vo.FileCategoryVo;
import com.zeng.niceservice.service.FileCategoryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class FileCategoryServiceImpl implements FileCategoryService {

    @Autowired
    FileCategoryMapper fileCategoryMapper;

    @Override
    public FileCategoryVo addOne(FileCategoryVo fileCategoryVo) {
    FileCategory fileCategory = FileCategory.builder().build();
    BeanUtils.copyProperties(fileCategoryVo, fileCategory, IgnoreStringUtil.ignoreExclude());
    int insert = fileCategoryMapper.insert(fileCategory);
    return BeanUtil.beanToVo(fileCategory, fileCategoryVo);
    }

    @Override
    public FileCategoryVo getOneByCode(String code) {

    FileCategory fileCategory = this.fileCategoryMapper.selectById(code);
    FileCategoryVo fileCategoryVo = BeanUtil.beanToVo(fileCategory, new FileCategoryVo());

    return fileCategoryVo;
    }

    @Override
    public Integer update(FileCategoryVo fileCategoryVo) {
    FileCategory fileCategory = BeanUtil.VoToBean(fileCategoryVo,
    new FileCategory(),
    IgnoreStringUtil.ignoreExclude(IgnoreConstant.code.getValue()));
    return this.fileCategoryMapper.updateById(fileCategory);
    }

    @Override
    public Integer delete(String code) {
    return this.fileCategoryMapper.deleteById(code);
    }

    @Override
    public Integer batchDelete(List<String> codes) {

    return this.fileCategoryMapper.deleteBatchIds(codes);
    }

    @Override
    public List<FileCategoryVo> findPage(Page page, FileCategoryVo fileCategoryVo) {
        FileCategory fileCategory = BeanUtil.VoToBean(fileCategoryVo, new FileCategory());
        this.fileCategoryMapper.selectPage(page, this.getWrapper(fileCategoryVo));
        List orders = page.getRecords();
        List<FileCategoryVo> fileCategoryVos = new ArrayList<>();
        orders.forEach(e->{
            fileCategoryVos.add(BeanUtil.beanToVo(e, new FileCategoryVo()));
        });
        page.setRecords(null);
        return fileCategoryVos;
        }


    @Override
    public Integer count(FileCategoryVo fileCategoryVo) {
        Integer integer = this.fileCategoryMapper.selectCount(this.getWrapper(fileCategoryVo));
        return integer;
    }

    public AbstractWrapper getWrapper(FileCategoryVo fileCategoryVo) {
        QueryWrapper<FileCategory> wrapper = new QueryWrapper<>();
        if (fileCategoryVo != null) {

                if (StringUtils.isNotEmpty(fileCategoryVo.getFlag())) {
                    wrapper.like("flag", fileCategoryVo.getFlag());
                }
                if (StringUtils.isNotEmpty(fileCategoryVo.getName())) {
                    wrapper.like("name", fileCategoryVo.getName());
                }

        }
        return wrapper;
    }
}
